<?php
	defined('_JEXEC') or die(); 
    JHTML::_('behavior.tooltip'); 
    $listOrder    = @$this->lists['order'];
    $listDirn    = @$this->lists['order_Dir'];
	$db = JFactory::getDbo();
	
?>
<style>
    #tbl_filter{
        margin: 0 ; padding: 0 ;
    }
    #tbl_filter td{
        padding: 3px;
    }
    #tbl_filter input, #tbl_filter img {
        float: left;
        margin: 0 5px 0 0;
        width: auto;
    }
    #extra_filter_field { width: 114px; }
    table.adminlist tbody tr td { padding: 8px 8px; }
    table.adminlist thead th { padding: 8px; }
</style>
<link rel="stylesheet" type="text/css" href="<?php echo JURI::root(); ?>js/multiselect/jquery.multiselect.css" />
<link rel="stylesheet" type="text/css" href="<?php echo JURI::root(); ?>js/multiselect/jquery-ui.css" />
<script type="text/javascript" src="<?php echo JURI::root(); ?>js/jquery.js"></script>
<script type="text/javascript" src="<?php echo JURI::root(); ?>js/jquery-ui.min.js"></script>
<script type="text/javascript" src="<?php echo JURI::root(); ?>js/multiselect/jquery.multiselect.js"></script>

<script type="text/javascript">
    function submitform(pressbutton){
        var form = document.adminForm ;
        //alert(pressbutton)
        if (pressbutton)
        {
            form.task.value=pressbutton ;
        }
        if ((pressbutton=='send_redemp_notice_email')||(pressbutton=='send_redemp_pick_up_notice')){
              form.controller.value="reports" ;
        }
        form.submit();
    }
	jQuery(function(){
		jQuery("#topDisplaySelect").change(function(){
			jQuery("#limit").val(jQuery(this).val());
			submitform();
		});
		jQuery("#topDisplaySelect").val(jQuery("#limit").val());
	});
    jQuery(function(){ 
        jQuery("#extra_products ").multiselect({
            selectedList: 0,
            noneSelectedText: '-- All --',
            selectedText: "# of # selected"
        }); 
        
    });
    
</script>
<div class="col width-100">    
    
        <div id="editcell">
            <form action="<?php echo JRoute::_("index.php?option=com_wrd&controller=reports")?>" method="post" name="filterform" >
                <table style='margin-bottom:5px' id="tbl_filter">
                    <tbody>
                        <tr>
                            <td align="left" valign="top" id="statistic_by_job" style="">
                                <table>                                  
                                    <tr>
                                        <td style="text-align: left;padding: 10px 3px;">
                                             Status:
                                        </td>
                                        <td>
                                            <?php echo $this->lists['extra_status']; ?>
                                        </td>
										<td align="left" valign="top" style="">
											<table >
												<tr>
													<td style="text-align: left;padding: 10px 3px;">
														 From:
													</td>
													<td>
														<?php
															echo JHTML::_ ( 'calendar', ($this->pr['date_from'] != '') ? $this->pr['date_from'] : date('Y-m-d', mktime(0, 0, 0, date('m'), 1, date('Y'))), 'date_from', 'date_from', '%Y-%m-%d');
														?>
													</td>
											  
													<td style="text-align: left;padding: 10px 3px;">To:</td>
													<td>
														<?php
															echo JHTML::_ ( 'calendar', ($this->pr['date_to'] != '') ? $this->pr['date_to'] : date('Y-m-d', time()), 'date_to', 'date_to', '%Y-%m-%d');
														?>
													</td>
												</tr>
											</table>                                
										</td>                            
										 
                                        <td>
                                             Product: 
                                        </td>
                                        <td>
                                            <?php echo $this->lists['extra_products'] ; ?>
                                        </td>
                                    </tr>
                                    
                                </table>
                            </td>
                            <td align="left" id="last_filter_column">
                                <table >
                                    <tr>
                                        <td>
                                            Choose: 
                                            <select name="topDisplaySelect" id="topDisplaySelect">
                                                <option value="10">5</option>
												<option value="10">10</option>
                                                <option value="10">15</option>
												<option value="20">20</option>
                                                <option value="50">30</option>
												<option value="50">50</option>
                                                <option value="100">100</option>
                                                <option value="0">All</option>
                                            </select>
                                        </td>
                                    </tr>
                                </table>
                            </td>                       
                        </tr>
						<tr><td colspan="2">
							<input type="submit" name="view" onclick="document.getElementById('submitViewExcel').value = 'submitViewExcel';  document.getElementById('task').value = 'redemptiondetail';" value="View" />	
							<input type="submit" name="summary" onclick="document.getElementById('submitViewExcel').value = 'submitViewExcel';  document.getElementById('task').value = 'redemptionsummary';" value="Summary" />
							<input type="submit" name="detail" onclick="document.getElementById('submitViewExcel').value = 'submitViewExcel';  document.getElementById('task').value = 'redemptiondetail';" value="Detail" />
							<input type="submit" name="exportexcel" onclick="document.getElementById('submitViewExcel').value = 'submitViewExcel';  document.getElementById('task').value = 'exredemptiondetail';" value="Export to excel" />
							
						</td>
						</td></tr>
                    </tbody>
                </table>
                <input type="hidden" name="controller" value="reports" />
                <input type="hidden" name="option" value="com_wrd" />
                <input type="hidden" name="submitViewExcel" id="submitViewExcel" value="" />
                <input type="hidden" name="task" id="task" value="" />
            </form>    
            <br />
            <form id="adminForm" action="<?php echo JRoute::_("index.php?option=com_wrd&controller=reports")?>" method="post" name="adminForm" >
<script type="text/javascript" src="<?php echo JURI::root(); ?>js/dialog/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript">
    jQuery(function(){ 
        jQuery('.pick-up-date').click(function(){
            var idMember = jQuery(this).attr("name").split("_");
            jQuery("#pick_up_date_member").val(idMember[1]);
            jQuery("#delivery_or_pick_up").val(1);
            jQuery('#dialog').dialog('open');
            return false;
        });
        
        jQuery('.deliver_to_outlet-date').click(function(){
            var idMember = jQuery(this).attr("name").split("_");
            jQuery("#pick_up_date_member").val(idMember[1]);
            jQuery("#delivery_or_pick_up").val(2);            
            jQuery('#dialog').dialog('open');
            return false;
        });

    });
</script>
<style>
.extra-adminlist strong { color:#025A8D; font-size: 1.2em; font-weight: bold; }
table.extra-adminlist .extra-adminlist-tr td {background: #F7F7F7; }
.filter-detail { border-bottom: 1px solid #CCC; padding-bottom: 3px; font-size: 1.4em; }
.filter-detail span { color: #e72222; }
</style>
<?php 
    $complainType = JHTMLwrd::listComplainTypeRaw();
    $mealPeriod = JHTMLwrd::listMealPeriodRaw();
?>
<script type="text/javascript">
jQuery(function(){
    jQuery("#statistic_by").html(jQuery("#statistic_by").html() + "<span style='color:#000;'>Products:</span> " + getSelectedOptionFilter("extra_products")); 
    jQuery("#statistic_by").html(jQuery("#statistic_by").html() + ". <span style='color:#000;'>Outlet:</span> " + getSelectedOptionFilter("pick_up_at"));
});

function getSelectedOptionFilter(listName)
{
    var html = "";
    jQuery("#"+listName+" option[selected=selected]").each(function(){
        html = html + jQuery(this).html() + ", ";
    });
    return html.substring(0, html.length-2);
}
</script>
<h3 class="filter-detail"><span id="statistic_by"></span><span style="color:#C00; float:right;">Total: <?php echo $this->pagination->total; ?></span></h3>
<table class="adminlist extra-adminlist">
  <thead>
    <tr class="extra-adminlist-tr" >
        <th nowrap="nowrap"><strong>No</strong></th>                   
		<th nowrap="nowrap"><strong>Item</strong></th>	
		<th nowrap="nowrap"><strong>Quantity</strong></th>
		<th nowrap="nowrap"><strong>Redeemption Code</strong></th>	
		<th nowrap="nowrap"><strong>Date Redeem</strong></th>
		<th nowrap="nowrap"><strong>Member ID</strong></th>
		<th nowrap="nowrap"><strong>Retail Pricing</strong></th>
		<th nowrap="nowrap"><strong>Cost Pricing</strong></th>	
	    <th colspan="3">
			<table>
				<tr>
					<th style="border: none;" width="50%"><strong>Outlet</strong></th>
					<th style="border: none;" align="left" width="15%"><strong>Percent</strong></th>
					<th style="border: none;" width="35%"><strong>Charge Cost</strong></th>
				</tr>
			</table>
		</th>
		
       
    </tr>
  </thead>
  <tbody>
    <?php 
        if(!empty($this->items)):
        {
            $rIds = array();
            $rId = '';
            $previousCode = '';
            $index = '';
            foreach ($this->items as $key => $item):
            
                if($previousCode == ''):    
                    $previousCode = $item->code;
                endif;
                
                if($item->code != $previousCode):
                    if(count($rIds) <= 0):
                        $rIds[0] = trim($rId, ',');
                    else:
                        $rIds[$index] = trim($rId, ',');
                    endif;
                    
                    $rId = '';
                    $index = '';
                endif;
                
                if($index == ''):
                    $index = $key;
                endif;
                $rId .= $item->id.',';    
              
            endforeach;
            $rIds[$index] = trim($rId, ',');
        }
        endif;
     ?>
    <?php 
        if(!empty($this->items)):
           
            $index = 0;
            $i=0;
            foreach($this->items as $key => $item):
                $i++;
                
                $tr = PointProvider::getTransactionByCode($db, array('code'=>strval($item->code)));
				$total = 0;
				foreach($tr as $its){
					$total += (- $its->point);
				}
				//echo '<pre>';print_r($tr);
                echo '<tr style="cursor:pointer;" class="row'.(($i+1)%2).'" valign="top">';
                    
                    echo '<td  style="">'.$i.'</td>';   
					echo '<td  style="width:10%">'.$item->product.'</td>'; 
					echo '<td style="text-align:right;">'.$item->quantity .'</td>';
					echo '<td style="text-align:right;">'.$item->code .'</td>';
					echo '<td style="text-align:right;">'.$item->exchange_date .'</td>';
					echo '<td style="text-align:right;">'.$item->member_id .'</td>';
					echo '<td style="text-align:right;">'.$item->price .'</td>';
					echo '<td style="text-align:right;">'.$item->cost_pricing .'</td>';					
                    echo '<td colspan="3"  style=""><table border="0" width="100%">';
					foreach($tr as $row):
						echo '<tr><td width="50%">';
								if($row->name != ''){
									echo $row->name;
								} 
								else if(($row->type == 9) && ($row->transaction == 0)){
									echo "WMCRewards - Start Point";
								}
								else if(($row->type == 0) &&($row->transaction == 0)){
									echo "WMCRewards";
								}
								else {
									$db = JFactory::getDbo();
									if(empty($row->name)){
										$sql = "SELECT des,MIN(id) FROM #__wrd_point WHERE transaction=".$row->transaction;
										$db->setQuery($sql);
										$des = $db->loadObjectList();
										echo "WMC GROUP - ".$des[0]->des;
									}
								}
						echo '</td><td width="15%">';
								echo number_format((-($row->point)/$total * 100),2, '.', ',') ."%";
						echo '</td><td width="25%" align="right">';
								echo number_format($item->quantity * $item->cost_pricing*(-($row->point)/$total),0, '.', ',');
						echo '</td></tr>';	
					endforeach;
					echo '</table></td>';
                echo '</tr>';
              
            endforeach;
        endif;
    ?>
  </tbody>
  <tfoot>
        <tr>
            <td colspan="16">
                <?php
                     echo $this->pagination->getListFooter();
                ?>
            </td>
        </tr>
    </tfoot>
</table>
                <input type="hidden" name="controller" value="reports" />
                <input type="hidden" name="option" value="com_wrd" />
                <input type="hidden" name="task" value="redemptiondetail" />
                <input type="hidden" name="boxchecked" value="0" />
                <?php echo JHTML::_( 'form.token' ); ?>
            </form>
        </div>
</div>